package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.XqHospital;
import com.xw.childhealth.service.XqHospitalService;
import com.xw.childhealth.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-20 15:50
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/xqHospital")
public class XqHospitalController {

    @Autowired
    private XqHospitalService xqHospitalService;

    @PostMapping("/getXqHospital")
    public ResultVO getXqHospital() {
        return ResultVOUtil.success(recursionData(new ArrayList<>(), 0));
    }


    /**
     * 查找下级节点目录
     *
     * @param dataDictionaryVOList 数据字典 List
     * @param id                   查找的上级 id
     * @return
     */
    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, Integer id) {

        List<XqHospital> parentNode = xqHospitalService.findByParentIdEquals(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return dataDictionaryVOList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setId(e.getId());
            dataDictionaryVO.setTitle(e.getInformation());

            if (e.getInformation() != null) {
                log.error("");
                List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(list.size() == 0 ? null : list);

                dataDictionaryVOList.add(dataDictionaryVO);
            }

        });

        return dataDictionaryVOList;
    }


    /**
     * 修改或者添加
     *
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type,
                             @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "xqCode", required = false) String xqCode,
                             @RequestParam(value = "code", required = false) String code,
                             @RequestParam(value = "xqName", required = false) String xqName,
                             @RequestParam(value = "isOpen", required = false) String isOpen,
                             @RequestParam(value = "ywCategory", required = false) String ywCategory,
                             @RequestParam(value = "xqHospital", required = false) String xqHospital,
                             @RequestParam(value = "xqFullName", required = false) String xqFullName,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/xq-hospital", map);
        }
        if (type.equals("toUpd")) {
            XqHospital one = xqHospitalService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            XqHospital one = xqHospitalService.findByIdEquals(nodeId);
            xqHospitalService.save(one.setXqCode(xqCode).setCode(code).setXqName(xqName).setIsOpen(isOpen == null ? 0 : 1).setYwCategory(ywCategory).setXqHospital(xqHospital).setXqFullName(xqFullName).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/xq-hospital", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            xqHospitalService.save(new XqHospital().setXqCode(xqCode).setCode(code).setXqName(xqName).setIsOpen(isOpen == null ? 0 : 1).setYwCategory(ywCategory).setXqHospital(xqHospital).setXqFullName(xqFullName).setUpdateTime(new Date()).setCreateTime(new Date()).setParentId(addRecordNode));
            return new ModelAndView("/pages/admin/xq-hospital", map);
        }
        return new ModelAndView("/pages/admin/xq-hospital-data-add", map);
    }


    /**
     * 查询父节点下面的子节点信息
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<XqHospital> result = xqHospitalService.findByParentIdEquals(nodeId);
        return ResultVOUtil.success(result);
    }

    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        XqHospital result = xqHospitalService.findByIdEquals(id);
        if (result != null) {
            xqHospitalService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/xz-code-maintain");
    }
}
